package com.jf.yytgd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf.yytgd.dao.LikeMapper;
import com.jf.yytgd.entity.bean.Like;
import com.jf.yytgd.entity.dto.like.LikeDTO;
import com.jf.yytgd.entity.dto.like.LikeExistDTO;
import com.jf.yytgd.service.LikeService;
import com.jf.yytgd.utils.ObjectUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA
 * Description: 点赞服务层实现
 * User:22954
 * Date:2025-02-06
 * Time:0:37
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {
    @Resource
    private LikeMapper likeMapper;

    @Override
    public boolean isLike(LikeExistDTO likeDTO) {
        boolean ret;
        if (likeDTO.hasEmpty()) {
            ret = false;
        } else {
            Like like = getLike(likeDTO);
            ret = like != null && !like.getDeleted();
        }
        return ret;
    }

    @Override
    public boolean like(LikeDTO likeDTO) {
        boolean ret;
        Like like = getLike(likeDTO);
        if (like == null) {
            like = new Like();
            BeanUtils.copyProperties(likeDTO, like);
            ret = true;
        } else {
            boolean deleted = like.getDeleted();
            like.setDeleted(!deleted);
            ret = deleted;
        }
        likeMapper.insertOrUpdate(like);
        return ret;
    }

    private Like getLike(Object likeDTO) {
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(ObjectUtil.nonEmptyFieldMap(likeDTO));
        return likeMapper.selectOne(queryWrapper);
    }
}
